//厦门大学计算机专业 | 前华为工程师
//专注《零基础学编程系列》  http://lblbc.cn/blog
//包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
//公众号：蓝不蓝编程
Page({
  data: {
    top: 86,
    left: 86,
    backgroundColor: '#FFFFFF',
    regionBG: "#FFC0C0",
    isOnAccelerometerChange: true,
    x: 0,
    y: 0,
    z: 0,
},
onLoad: function (options) {
    var that = this
    wx.startGyroscope({
      interval: "normal",
      success: function () {
        wx.onAccelerometerChange(function (res) {
          if (!that.data.isOnAccelerometerChange) {
            return
          }
          var x = (res.x * 180).toFixed(2)
          var y = -(res.y * 180).toFixed(2)
          var z = (res.z * 180).toFixed(2)
          var top = 86 - Number(y)
          var left = 86 - Number(x)
          if (top < 0) {
            top = 0
          }
          if (top > 170) {
            top = 170
          }
          if (left < 0) {
            left = 0
          }
          if (left > 170) {
            left = 170
          }
          that.setData({
            x: x,
            y: y,
            z: z,
            top: top,
            left: left
          })
          if (top > 76 && top < 105 && left > 76 && left < 109) {
            that.setData({
              regionBG: "#82ec82",
            })
          } else {
            that.setData({
              regionBG: "#FFC0C0",
            })
          }
        })
      }
    })
 
},
onUnload: function () {
    this.setData({
      isOnAccelerometerChange: false
    })
    wx.stopAccelerometer({
    })
    wx.stopGyroscope({
    })
},
})